import { Client } from "@microsoft/microsoft-graph-client";
import { AadTokenProvider } from '@microsoft/sp-http';

// 获取 Graph 客户端
export const getGraphClient = async (context) => {
  try {
    // 获取 AAD 令牌提供程序
    const tokenProvider = await context.aadTokenProviderFactory.getTokenProvider();
    
    // 创建 Graph 客户端
    const client = Client.init({
      authProvider: (done) => {
        // 获取访问令牌
        tokenProvider.getToken("https://graph.microsoft.com")
          .then((token) => {
            done(null, token);
          })
          .catch((error) => {
            done(error, null);
          });
      }
    });

    return client;
  } catch (error) {
    console.error("Error initializing Graph client:", error);
    throw error;
  }
};

// 检查用户权限
export const checkUserPermissions = (context) => {
  return context.pageContext.web.permissions.hasPermission;
}; 